package com.ttmall.controller;

import com.ttmall.common.pojo.EasyUIDataGridResult;
import com.ttmall.common.pojo.TaotaoResult;
import com.ttmall.pojo.TbContent;
import com.ttmall.service.ContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by GalaIO on 2018/1/18.
 */
@RestController
@RequestMapping("/content")
public class ContentController {
    @Autowired
    private ContentService contentService;

    @RequestMapping("/{id}")
    public TaotaoResult getContentById(@PathVariable Long id) {
        return contentService.getContentById(id);
    }

    @RequestMapping("/query/list")
    public EasyUIDataGridResult listConten(Long categoryId, Integer page, Integer rows) {
        return contentService.listContentByContentCatId(categoryId, page, rows);
    }

    @RequestMapping("/save")
    public TaotaoResult saveContent(TbContent tbContent) {
        return contentService.insertContent(tbContent);
    }

    @RequestMapping("/edit")
    public TaotaoResult editContent(TbContent content) {
        return contentService.updateContent(content);
    }

    @RequestMapping("/delete")
    public TaotaoResult deleteContent(String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            String[] idSplit = ids.split(",");
            List<Long> idLongs = new ArrayList<>();
            for (String s : idSplit) {
                try {
                    idLongs.add(new Long(s));
                } catch (NumberFormatException ex) {
                    //do nothing
                }
            }
            if (idLongs.size() > 0) {
                return contentService.deleteContent(idLongs);
            }
        }
        return TaotaoResult.build(400, "error params!");
    }

}
